refactor: unify generate() and stream() methods to base Client class#13
Conversation
Pull Request Review: Unify generate() and stream() MethodsSummaryThis PR refactors the base Client class to consolidate common logic for generate() and stream() methods, reducing code duplication across providers. The refactoring introduces several new abstract methods that providers must implement, creating a cleaner separation of concerns. Code Quality & ArchitectureStrengths
Areas for Improvement1. Missing _transform_output Reference in generate()Location: src/celeste/client.py:56-60 The stream() method references self._transform_output at line 84, but generate() doesn't apply output transformations. This creates an inconsistency between streaming and non-streaming paths. Recommendation: Apply _transform_output in generate() as well to ensure consistent behavior. 2. Type Ignore Comment Needs JustificationLocation: src/celeste/client.py:82 The type: ignore comment suggests a type mismatch that should either be fixed or documented. 3. Streaming Field Initialization in Model RegistryLocation: src/celeste/models.py:55 When merging models with the same (id, provider) key, the streaming field is only set during initial registration and never updated. If the same model is registered multiple times with different streaming values, only the first value will be used. Recommendation: Consider validating consistency or documenting the expected behavior. Potential BugsCritical Issue: Error Handling in generate()Location: src/celeste/client.py:53-55 Line 55 calls response.json() without error handling. If the response body isn't valid JSON, this will raise a JSONDecodeError that isn't caught. Recommendation: Wrap the json() call in try-except to provide better error messages. Performance ConsiderationsGood Performance Design
Security ConcernsSecurity Strengths
Minor Security ConsiderationLocation: src/celeste/client.py:153 Using response.text as a fallback could potentially expose sensitive information. Consider truncating to 200 characters. Test CoverageTest Updates Look GoodThe test file updates properly implement all new abstract methods with reasonable mock implementations. Missing Test CoverageConsider adding tests for:
Overall AssessmentThis is a well-designed refactoring that significantly improves code organization and maintainability. The Template Method pattern is appropriately applied, and the separation between framework and provider logic is clean. Priority Action Items:
Recommendation: Approve with Minor ChangesThe core architecture is sound, but the output transformation inconsistency should be addressed before merging to ensure consistent behavior between generate() and stream(). Great work on this refactoring! The codebase will be much more maintainable going forward. |
Summary
This PR unifies the
generate()andstream()methods in the baseClientclass, reducing code duplication across providers and improving maintainability.Changes Made
Core Architecture
generate()method: Moved common orchestration logic to baseClientclassstream()method: Moved common streaming logic to baseClientclass_create_inputs(),_make_request(),_output_class(),_stream_class(),_make_stream_request()streaming: boolfield toModelclass to indicate per-model streaming supportFiles Changed
src/celeste/client.py: Unified generate/stream methods, added abstract methodssrc/celeste/models.py: Added streaming field to Model classsrc/celeste/artifacts.py: Improved docstring for Artifact classtests/unit_tests/test_client.py: Updated ConcreteClient to implement new abstract methodsBenefits